#include "StringManager.h"

#include "cocos2d.h"

#include "GameState.h"
#include "Player.h"

using namespace cocos2d;


void ReadFileToTextArray(char* filename, int array_max, char** text_array)
{
	char temp[1024];

	//text_array = new char*[array_max];

	std::string fullPath = CCFileUtils::fullPathFromRelativeFile(filename, filename);
	unsigned char* pBuffer = NULL;
	unsigned long bufferSize = 0;
	pBuffer = CCFileUtils::getFileData(fullPath.c_str(), "r", &bufferSize);

	int id = 0;
	int cnt = 0;

	for(int i = 3; i < bufferSize; i++)	//3byte is endian
	{
		if(pBuffer[i] == ';')
		{
			temp[cnt] = NULL;
			text_array[id] = new char[cnt + 1];
			strcpy(text_array[id], temp);
			
			id++;
			cnt = 0;

			if(id >= array_max)
			{
				break;
			}
			else
			{
				continue;
			}
		}

		temp[cnt] = pBuffer[i];
		cnt++;
	}
}

StringManager* StringManager::pInstance = NULL;

StringManager& StringManager::getInstance()
{
	if(pInstance == NULL) pInstance = new StringManager;
	return *pInstance;
}

StringManager::StringManager()
{
	//Read Player Names
	player_name = new char*[PLAYER_MAX];
	ReadFileToTextArray("player_name_list.txt", PLAYER_MAX, player_name);

	//Read Skill Names
	skill_name = new char*[ds_max];
	ReadFileToTextArray("dungeon_skill_list.txt", ds_max, skill_name);
}

StringManager::~StringManager()
{
	//Delete Skill Names
	for(int i = 0; i < ds_max; i++)
	{
		delete [] skill_name[ds_max];
		skill_name[ds_max] = NULL;
	}

	delete [] skill_name;
	skill_name = NULL;
}
